package com.tools.web.interceptor.quota;

import com.tools.common.object.Note;
import com.tools.web.work.ResultCode;

import java.util.Map;
import java.util.Objects;

/**
 * 配额限制拦截器所需的参数配置项的包装类实例
 * */
@Note("配额限制拦截器所需的参数配置项的包装类实例")
public class QuotaSource {

    @Note("缓存 key 的前缀，后续拼接上 uniqueID 作为 key")
    private String saveKeyPrefix;

    @Note("到达额度后响应给客户端的提示信息")
    private String quotaErrorMessage;

    @Note("没有找到 UniqueID 参数所响应的异常信息")
    private String notHaveUniqueIDErrorMessage;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public QuotaSource() {
        this.saveKeyPrefix = "quota:limit:";
        this.quotaErrorMessage = ResultCode.QUOTA_LIMIT.toJSON();
        this.notHaveUniqueIDErrorMessage = ResultCode.PARAM_ERROR.toJSON();
    }

    public static QuotaSource of() {
        return new QuotaSource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static QuotaSource from(Map properties) {
        QuotaSource source = new QuotaSource();
        if(properties == null || properties.isEmpty()) return source;
        source.setSaveKeyPrefix((String) properties.get("quota-manager-saveKeyPrefix"));
        source.setQuotaErrorMessage((String) properties.get("quota-manager-quotaErrorMessage"));
        source.setNotHaveUniqueIDErrorMessage((String) properties.get("quota-manager-notHaveUniqueIDErrorMessage"));
        return source;
    }

    /* **************************************************************************************
     *
     *          Getter / Setter
     *
     * **************************************************************************************
     * */

    public String getSaveKeyPrefix() {
        return saveKeyPrefix;
    }

    public void setSaveKeyPrefix(String saveKeyPrefix) {
        if(saveKeyPrefix != null && !saveKeyPrefix.isEmpty()) {
            this.saveKeyPrefix = saveKeyPrefix;
        }
    }

    public String getQuotaErrorMessage() {
        return quotaErrorMessage;
    }

    public void setQuotaErrorMessage(String quotaErrorMessage) {
        if(quotaErrorMessage != null && !quotaErrorMessage.isEmpty()) {
            this.quotaErrorMessage = quotaErrorMessage;
        }
    }

    public String getNotHaveUniqueIDErrorMessage() {
        return notHaveUniqueIDErrorMessage;
    }

    public void setNotHaveUniqueIDErrorMessage(String notHaveUniqueIDErrorMessage) {
        if(notHaveUniqueIDErrorMessage != null && !notHaveUniqueIDErrorMessage.isEmpty()) {
            this.notHaveUniqueIDErrorMessage = notHaveUniqueIDErrorMessage;
        }
    }

    @Override
    public String toString() {
        return "QuotaSource{" +
                "saveKeyPrefix=" + saveKeyPrefix +
                ", quotaErrorMessage=" + quotaErrorMessage +
                ", notHaveUniqueIDErrorMessage=" + notHaveUniqueIDErrorMessage +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        QuotaSource that = (QuotaSource) o;
        return Objects.equals(saveKeyPrefix, that.saveKeyPrefix) && Objects.equals(quotaErrorMessage, that.quotaErrorMessage) && Objects.equals(notHaveUniqueIDErrorMessage, that.notHaveUniqueIDErrorMessage);
    }

    @Override
    public int hashCode() {
        return Objects.hash(saveKeyPrefix, quotaErrorMessage, notHaveUniqueIDErrorMessage);
    }

    public QuotaSource saveKeyPrefix(String saveKeyPrefix) {
        this.setSaveKeyPrefix(saveKeyPrefix);
        return this;
    }

    public QuotaSource quotaErrorMessage(String quotaErrorMessage) {
        this.setQuotaErrorMessage(quotaErrorMessage);
        return this;
    }

    public QuotaSource notHaveUniqueIDErrorMessage(String notHaveUniqueIDErrorMessage) {
        this.setNotHaveUniqueIDErrorMessage(notHaveUniqueIDErrorMessage);
        return this;
    }
}
